import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/network/http.dart';
import 'package:fzcgx_app/router/routes.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/login/page/login_page.dart';
import 'package:fzcgx_app/ui/order/model/order_detail_model.dart';
import 'package:fzcgx_app/ui/order/page/order_confirm_page.dart';
import 'package:nav_router/nav_router.dart';

class OrderDetailViewModel extends BaseViewModel {
  ScrollController controller;
  TabController tabController;
  bool isExpanded;
  OrderDetailModel model;
  int tabIndex = 0;
  double webViewHeight = 100.00;
  OrderDetailViewModel(this.tabController) {
    controller = ScrollController();
    isExpanded = false;
    controller.addListener(() {
      // 打印滚动位置
      print(controller.offset);

      if (controller.offset < 270) {
        if (isExpanded) {
          isExpanded = false;
          notifyListeners();
        }
      } else {
        if (!isExpanded) {
          isExpanded = true;
          notifyListeners();
        }
      }
      // if (controller.offset < 374) {
      //   if (this.tabIndex > 0) {
      //     updateTabIndex(0);
      //   }
      // }
      if (controller.offset >= this.webViewHeight + 150 + 375) {
        if (this.tabIndex != 3) {
          updateTabIndex(3);
        }
      } else if (controller.offset >= 270 + 150 + this.webViewHeight) {
        if (this.tabIndex != 2) {
          updateTabIndex(2);
        }
      } else if (controller.offset >= 270 + 150) {
        if (this.tabIndex != 1) {
          updateTabIndex(1);
        }
      } else {
        if (this.tabIndex != 0) {
          updateTabIndex(0);
        }
      }
    });
    this.setState(ViewState.Loading);
  }
  void requestApi(String id) {
    Http.instance.post<OrderDetailModel>('home/bale-one', {'id': id},
        onSuccess: (data) {
      if (data != null) {
        this.model = data;
        this.setState(ViewState.Success);
      } else {
        this.model = OrderDetailModel();
        this.setState(ViewState.Empty);
      }
    }, onError: (error) {
this.setState(ViewState.Error);
    }, onFail: (fail) {
this.setState(ViewState.Error);
    });
  }

  void updateTabIndex(int index) {
    this.tabController.animateTo(index);
    this.tabIndex = index;
    notifyListeners();
  }

  void callBack(int code, String msg, content) {
    if (code == 201) {
      this.webViewHeight = content;
      notifyListeners();
    }
  }

  void didTabIndex(int index) {
    if (index == 0) {
      controller.animateTo(
        0,
        duration: const Duration(milliseconds: 300),
        curve: Curves.easeOut,
      );
    } else if (index == 1) {
      controller.animateTo(
        150.0 + 270,
        duration: const Duration(milliseconds: 300),
        curve: Curves.easeOut,
      );
    } else if (index == 2) {
      controller.animateTo(
        270 + 150 + this.webViewHeight,
        duration: const Duration(milliseconds: 300),
        curve: Curves.easeOut,
      );
    } else {
      controller.animateTo(
        controller.position.maxScrollExtent, //滚动到底部
        duration: const Duration(milliseconds: 300),
        curve: Curves.easeOut,
      );
    }
  }

  void onQiangDan(BuildContext context) {
    String uid = SpUtil.getString('uid');
    if (uid.isEmpty) {
      Routes.navigateTo(context, LoginPage());
      return;
    }
    routePush(OrderConfirmPage(id: this.model.id,));
    
  }
  void  hideDisplayBar(){
    this.model.ispop=0;
    notifyListeners();
    Http.instance.post('home/bale-one-popno', {'id':this.model.id},
        onSuccess: (data) {
         
        },
        onError: (error) {},
        onFail: (fail) {},
        complete: () {
          
        });
  }
}
